/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisy.analysis;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class LocusPathsStatistics {

    private final Map<String, Integer> pathCounterMap = new HashMap<String, Integer>();

    public void addPath(String path) {
        Integer counter = getCounter(path);
        Integer newCounter = Integer.valueOf(++counter);
        pathCounterMap.put(path, newCounter);
    }

    private Integer getCounter(String path) {
        Integer counter = pathCounterMap.get(path);
        if (counter == null) {
            counter = Integer.valueOf(0);
            pathCounterMap.put(path, counter);
        }
        return counter;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (String path : pathCounterMap.keySet()) {
            //result.append(pathToString(path)).append(":").append(pathCounterMap.get(path)).append('\n');
            //result.append(pathCounterMap.get(path)).append(". ").append(pathToString(path)).append('\n');
            result.append(pathCounterMap.get(path)).append("\u00D7 ").append(pathToString(path)).append('\n');
        }
        return result.toString();
    }

    private String pathToString(String path) {
        String[] parents = path.split(":");
        StringBuilder result = new StringBuilder(parents.length * 2);
        for (int i = 0; i < parents.length; i++) {
            result.append('/').append(parents[i]);
        }
        return result.toString();
    }


}
